package com.obas.integration.element.telnet.editor;

import com.ease.common.util.Detect;
import com.obas.integration.element.telnet.type.HWMacAddressType;
import com.obas.integration.element.telnet.type.HWPortFeatureType;
import com.obas.model.business.DeviceMacAddress;

public class HWS9303TelnetEditor extends AbstractTelnetEditor {

	// TYPE MAC MAC TYPE F/S VLAN ID
	// eth 781d-ba30-f699 dynamic 0/17A 3002
	public DeviceMacAddress editAsText(String text) {

		String macAddress = formatString(text.substring(0, 15));
		String vlanVsiSi = formatString(text.substring(15, 27));
		String peVlan = formatString(text.substring(17, 34));
		String ceVlan = formatString(text.substring(33, 41));
		String port = formatString(text.substring(41, 57));
		String type = formatString(text.substring(57, 67));
		String lspMacTunnel = formatString(text.substring(67, 79));

		DeviceMacAddress deviceMacAddress = new DeviceMacAddress();

		HWPortFeatureType hWPortFeatureType = HWPortFeatureType.getByText(substring(port, 0, 2));
		deviceMacAddress.setPortFeatureType(null != hWPortFeatureType ? hWPortFeatureType.getPortFeatureType() : null);

		deviceMacAddress.setMacAddress(asMacAddress(macAddress));

		HWMacAddressType hWMacAddressType = HWMacAddressType.getByText(type);
		deviceMacAddress.setMacAddressType(null != hWMacAddressType ? hWMacAddressType.getMacAddressType() : null);

		deviceMacAddress.setPortCode(port);
		deviceMacAddress.setVlan(vlanVsiSi);

		return deviceMacAddress;
	}

	public static String substring(String value, int beginIndex, int endIndex) {
		if (Detect.notEmpty(value) && value.length() >= endIndex) {
			return value.substring(beginIndex, endIndex);
		}
		return null;
	}
}
